
package com.rt.schedulebase.core.websocket;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.rt.schedulenew.utils.netty.service.NettySocketIOService;
import org.springframework.stereotype.Component;


@Component
public class NettyPushServer {
    private Logger logger = LoggerFactory.getLogger(this.getClass().getName());

    @Autowired(required = false)
    NettySocketIOService nettySocketIOService;

    int code;

    public NettyPushServer() {
        code = 1;
    }

    public void sendMessageToUser(String userId, String message) {
        if (StringUtils.isEmpty(userId)) {
            return;
        }
        boolean isConnection = nettySocketIOService.hasConnection(userId);
        if (isConnection) {
            try {
                nettySocketIOService.sendMessageToUser(userId, code, message);
            } catch (Exception e) {
                logger.error("netty-push:error", e);
                e.printStackTrace();
            }
        }
    }

    public void sendMessageToAllUsers(String message) {
        try {
            nettySocketIOService.sendMessageToAllUsers(code, message);
        } catch (Exception e) {
            logger.error("netty-push:error", e);
            e.printStackTrace();
        }
    }
}
